package com.gwsoft.mm.file;

import java.io.File;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.springframework.stereotype.Service;


/**
 * 生成下载文件模版的类
 * @author xuqing
 *
 */
@Service("fileHandlerUtils")
public class FileHandlerUtils {
	
	private static final String DOWNLOAD_FILE_PATH=ConfigLoader.getString("sms.temp.dir");
	private static final String UPLOAD_PATH = ConfigLoader.getString("upload.path");	

	public String createFileTemplate(String[] fieldArray){
		String filePath = "";
		try {
			String fileName = "template"+System.currentTimeMillis()+".xls";
			String sheetName = "template";
//			String[] fieldArray = {"省份ID","资源ID","排序号","创建时间"};
			
			File tmpDir = new File(UPLOAD_PATH+"/"+DOWNLOAD_FILE_PATH);
			if (!tmpDir.isDirectory()) {
				tmpDir.mkdirs();
			}
			File tmpFile = new File(UPLOAD_PATH+"/"+DOWNLOAD_FILE_PATH+"/"+fileName);
			WritableWorkbook book = Workbook.createWorkbook(tmpFile);
			WritableSheet sheet = book.createSheet(sheetName, 0);
			Label label = null;
			
			for (int i=0;i<fieldArray.length;i++) {
				label = new Label(i, 0, fieldArray[i]);
				sheet.addCell(label);
			}
			
			filePath = tmpFile.getPath();
			book.write();
			book.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return filePath;
	}
}
